/*

Replication code for Ben Cormier and Natalya Naqvi, "Delegating Discipline" forthcoming. Journal of Politics. 
Code for Tables and Figures in Main Text.
Note: requires estout package

*/

**************
*Data and set*
**************
use "data1_redacted.dta", clear
xtset country_num year
set scheme s1mono
grstyle init
grstyle set plain, nogrid 

******************************************
*Table 1: Simple Initial DID estimation
*Just keep and model eligible, dont' do a selection step
******************************************
keep if Eligible_LorMIC_2yrsRunning == 1
regress ln_gt1yr_byYear HardInclusionDummy i.year i.country_num, robust
estimates store did1
*Table 1
esttab did1 using Table1_Initial.csv, replace /// 
varlabels("HardInclusionDummy" "IndexInclusion") ///
cells(b(star fmt(%9.3f)) se(par)) stats(N) starlevels(* 0.05 ** 0.01 *** 0.001) nonumbers legend /// 
title("Table 1: Dummy Model of Bond Issuance") /// 
note("Country and Year fixed effects; Robust Standard Errors")


******************************************
*Table 2: Identifying initially-significant covariates 
*Reset to full data from above to run full selection model
******************************************
use "data1_redacted.dta", clear
xtset country_num year
set scheme s1mono
grstyle init
grstyle set plain, nogrid 
*Model
heckman issuancePerOfExtBwng l.GDPpcap l.GDPgrowth l.Inflation l.ExtDebtStock l.ka_open l.FinancialDepth l.Def_PerGDP l.Crtg_BestOf l.v2x_libdem l.v2cltrnslw l.LeftNew2yrs l.annavg_UStrasuryIntRate i.country_num i.year, ///
select(Eligible_LorMIC_2yrsRunning = l.GDPpcap l2.GDPpcap l3.GDPpcap l.Crtg_BestOf l2.Crtg_BestOf l3.Crtg_BestOf post2005dummy l.HardInclusionDummy) twostep
est store initialCorrelations
*Table 2
esttab initialCorrelations using Tab3_Initial.csv, replace /// 
varlabels("HardInclusionDummy" "IndexInclusion" "Def_PerGDP" "Deficit" "v2x_libdem" "Democracy" "Crtg_BestOf" "CreditRating" "v2cltrnslw" "Transparency" "ExtDebtStock" "DebtLevels" "annavg_UStrasuryIntRate" "USIRates" "LeftNew2yrs" "NewLeftGovt" "FinancialDepth" "DomFinDepth") ///
cells(b(star fmt(%9.3f)) se(par)) stats(N) starlevels(* 0.05 ** 0.01 *** 0.001) nonumbers legend /// 
title("Table 3: Initial Model of Market Access in EMBIG-Eligible Countries") /// 
note("Country and Year fixed effects" "Heckman twostep standard errors" "All variables lagged")


******************************************
*Table 4 and Figure 1: Dummy Interaction Models and Figures
******************************************
*Interaction Model (With Index Dummy)
heckman issuancePerOfExtBwng i.HardInclusionDummy##c.l.GDPpcap l.GDPgrowth l.Inflation i.HardInclusionDummy##c.l.ExtDebtStock l.ka_open l.FinancialDepth i.HardInclusionDummy##c.l.Def_PerGDP l.Crtg_BestOf l.v2x_libdem l.v2cltrnslw i.HardInclusionDummy##c.l.LeftNew2yrs i.HardInclusionDummy##c.l.annavg_UStrasuryIntRate  i.country_num i.year, ///
select(Eligible_LorMIC_2yrsRunning = l.GDPpcap l2.GDPpcap l3.GDPpcap l.Crtg_BestOf l2.Crtg_BestOf l3.Crtg_BestOf post2005dummy l.HardInclusionDummy) twostep
est store dummyMain

*Table 4
esttab dummyMain using Tab4_dummyMain.csv, replace /// 
varlabels("HardInclusionDummy" "IndexInclusion" "Def_PerGDP" "Deficit" "v2x_libdem" "Democracy" "Crtg_BestOf" "CreditRating" "v2cltrnslw" "Transparency" "ExtDebtStock" "DebtLevels" "annavg_UStrasuryIntRate" "USIRates" "LeftNew2yrs" "NewLeftGovt" "FinancialDepth" "DomFinDepth") ///
cells(b(star fmt(%9.3f)) se(par)) stats(N) starlevels(* 0.05 ** 0.01 *** 0.001) nonumbers legend /// 
title("Table 4: Dummy Interaction Models") /// 
note("Country and Year fixed effects" "Heckman twostep standard errors" "All variables lagged")

*Figure 1 Average Marginal Effect Plots (the five plots made below combined into one figure in Figure 1)
coefplot dummyMain, keep(*GDPpcap) vertical yline(0, lc(black)) xlabel(1 "Excluded" 2 "Included", tlength(0) nogrid notick) xscale(lstyle(none)) ///
title("") legend(rows(1) span size(small)) note("95% confidence intervals", size(small)) levels(95) ///
ytitle("Avg. ME on Bond Issuance") xtitle("") plotlabels("GDP per capita")
graph export "AMEs1_gdp.pdf", replace

coefplot dummyMain, keep(*ExtDebtStock) vertical yline(0, lc(black)) xlabel(1 "Excluded" 2 "Included", tlength(0) nogrid notick) xscale(lstyle(none)) ///
title("") legend(rows(1) span size(small)) note("95% confidence intervals", size(small)) levels(95) ///
ytitle("Avg. ME on Bond Issuance") xtitle("") plotlabels("Debt Levels")
graph export "AMEs2_debtlevels.pdf", replace

coefplot dummyMain, keep(*Def_PerGDP) vertical yline(0, lc(black)) xlabel(1 "Excluded" 2 "Included", tlength(0) nogrid notick) xscale(lstyle(none)) ///
title("") legend(rows(1) span size(small)) note("95% confidence intervals", size(small)) levels(95) ///
ytitle("Avg. ME on Bond Issuance") xtitle("") plotlabels("Deficit Size")
graph export "AMEs3_deficits.pdf", replace

coefplot dummyMain, keep(*LeftNew2yrs) relocate(incXdebt = 2 weightXdebt = 2) vertical yline(0, lc(black)) xlabel(1 "Excluded" 2 "Included", tlength(0) nogrid notick) xscale(lstyle(none)) ///
title("") legend(rows(1) span size(small)) note("95% confidence intervals", size(small)) levels(95) ///
ytitle("Avg. ME on Bond Issuance") xtitle("") plotlabels("New Left Govt")
graph export "AMEs4_newLeftGovt.pdf", replace

coefplot dummyMain, keep(*annavg_UStrasuryIntRate) relocate(incXdebt = 2 weightXdebt = 2) vertical yline(0, lc(black)) xlabel(1 "Excluded" 2 "Included", tlength(0) nogrid notick) xscale(lstyle(none)) ///
title("") legend(rows(1) span size(small)) note("95% confidence intervals", size(small)) levels(95) ///
ytitle("Avg. ME on Bond Issuance") xtitle("") plotlabels("US Interest Rates")
graph export "AMEs5_liquidity.pdf", replace


******************************************
*Table 5 and Figure 2: Weight Interaction Models and Figures
******************************************
*Interaction Model (With Index Weights)
heckman issuancePerOfExtBwng c.hardIndex_AnnAvg##c.l.GDPpcap l.GDPgrowth l.Inflation c.hardIndex_AnnAvg##c.l.ExtDebtStock l.ka_open l.FinancialDepth c.hardIndex_AnnAvg##c.l.Def_PerGDP l.Crtg_BestOf l.v2x_libdem l.v2cltrnslw c.hardIndex_AnnAvg##c.l.LeftNew2yrs c.hardIndex_AnnAvg##c.l.annavg_UStrasuryIntRate i.country_num i.year, ///
select(Eligible_LorMIC_2yrsRunning = l.GDPpcap l2.GDPpcap l3.GDPpcap l.Crtg_BestOf l2.Crtg_BestOf l3.Crtg_BestOf post2005dummy l.HardInclusionDummy) twostep
est store weightMain

*Table 5
esttab weightMain using Tab5_weightMain.csv, replace /// 
varlabels("HardInclusionDummy" "IndexInclusion" "Def_PerGDP" "Deficit" "v2x_libdem" "Democracy" "Crtg_BestOf" "CreditRating" "v2cltrnslw" "Transparency" "ExtDebtStock" "DebtLevels" "annavg_UStrasuryIntRate" "USIRates" "LeftNew2yrs" "NewLeftGovt" "FinancialDepth" "DomFinDepth") ///
cells(b(star fmt(%9.3f)) se(par)) stats(N) starlevels(* 0.05 ** 0.01 *** 0.001) nonumbers legend /// 
title("Table 5: Weight Interaction Models") /// 
note("Country and Year fixed effects" "Heckman twostep standard errors" "All variables lagged")

*Figure 2 Adjusted Marginal Effect Plots (the five plots made below combined into one figure in Figure 2) 
heckman issuancePerOfExtBwng c.hardIndex_AnnAvg##c.l.GDPpcap l.GDPgrowth l.Inflation c.hardIndex_AnnAvg##c.l.ExtDebtStock l.ka_open l.FinancialDepth c.hardIndex_AnnAvg##c.l.Def_PerGDP l.Crtg_BestOf l.v2x_libdem l.v2cltrnslw c.hardIndex_AnnAvg##c.l.LeftNew2yrs c.hardIndex_AnnAvg##c.l.annavg_UStrasuryIntRate i.country_num i.year, ///
select(Eligible_LorMIC_2yrsRunning = l.GDPpcap l2.GDPpcap l3.GDPpcap l.Crtg_BestOf l2.Crtg_BestOf l3.Crtg_BestOf post2005dummy l.HardInclusionDummy) twostep

margins, dydx(l.GDPpcap) at(hardIndex_AnnAvg=(0(1)20)) level(95)
marginsplot, noseparator yline(0, lc(black) lpattern(solid)) xscale(lstyle(none)) xlabel(,notick) recast(line) plot1opts(lpattern(dot)) recastci(rarea) ciopt(color(black%20)) ///
title("GDP per capita", size (mediumsmall)) note("95% confidence intervals", size(small)) ///
ytitle("Adj. ME on Bond Issuance") xtitle("Index Weight %")
graph export "AdjME1_GDP.pdf", replace

margins, dydx(l.ExtDebtStock) at(hardIndex_AnnAvg=(0(1)20)) level(95)
marginsplot, noseparator yline(0, lc(black) lpattern(solid)) xscale(lstyle(none)) xlabel(,notick) recast(line) plot1opts(lpattern(dot)) recastci(rarea) ciopt(color(black%20)) ///
title("Debt Levels", size (mediumsmall)) note("95% confidence intervals", size(small)) ///
ytitle("Adj. ME on Bond Issuance") xtitle("Index Weight %")
graph export "AdjME2_Debt.pdf", replace

margins, dydx(l.Def_PerGDP) at(hardIndex_AnnAvg=(0(1)20)) level(95)
marginsplot, noseparator yline(0, lc(black) lpattern(solid)) xscale(lstyle(none)) xlabel(,notick) recast(line) plot1opts(lpattern(dot)) recastci(rarea) ciopt(color(black%20)) ///
title("Deficit Size", size (mediumsmall)) note("95% confidence intervals", size(small)) ///
ytitle("Adj. ME on Bond Issuance") xtitle("Index Weight %")
graph export "AdjME3_Deficits.pdf", replace

margins, dydx(l.LeftNew2yrs) at(hardIndex_AnnAvg=(0(1)20)) level(95)
marginsplot, noseparator yline(0, lc(black) lpattern(solid)) xscale(lstyle(none)) xlabel(,notick) recast(line) plot1opts(lpattern(dot)) recastci(rarea) ciopt(color(black%20)) ///
title("New Left Govt", size (mediumsmall)) note("95% confidence intervals", size(small)) ///
ytitle("Adj. ME on Bond Issuance") xtitle("Index Weight %")
graph export "AdjME4_NewLeftGovt.pdf", replace

margins, dydx(l.annavg_UStrasuryIntRate) at(hardIndex_AnnAvg=(0(1)20)) level(95)
marginsplot, noseparator yline(0, lc(black) lpattern(solid)) xscale(lstyle(none)) xlabel(,notick) recast(line) plot1opts(lpattern(dot)) recastci(rarea) ciopt(color(black%20)) ///
title("US Interest Rates", size (mediumsmall)) note("95% confidence intervals", size(small)) ///
ytitle("Adj. ME on Bond Issuance") xtitle("Index Weight %")
graph export "AdjME5_Liquidity.pdf", replace

